package com.nondo.dean.dealer.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.dealer.domain.ActivityTemplate;
import com.nondo.dean.dealer.domain.ActivityTemplateExample;
import com.nondo.dean.dealer.mapper.ActivityTemplateMapper;

/**
 * @author xiaofan
 * @Description: 活动模板业务管理类
 * @throws
 * @date 2018-05-0911:23
 * @Version 0.1
 */
@Component
public class ActivityTemplateService {

    @Autowired
    private ActivityTemplateMapper activityTemplateMapper;

    /**
     * 添加模板
     * @param activityTemplate
     * @return
     */
    public int add(ActivityTemplate activityTemplate){
        return activityTemplateMapper.insertSelective(activityTemplate);
    }

    /**
     * 查询模板不分页列表
     * @return
     */
    public List<ActivityTemplate> selectList(){
        ActivityTemplate activityTemplate = new ActivityTemplate();
        ActivityTemplateExample activityTemplateExample =new ActivityTemplateExample();
        ActivityTemplateExample.Criteria criteria= activityTemplateExample.createCriteria();
        setCriteria(criteria,activityTemplate);

       return  activityTemplateMapper.selectByExample(activityTemplateExample);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param
     */
    void setCriteria(ActivityTemplateExample.Criteria criteria, ActivityTemplate activityTemplate) {
        if (activityTemplate != null) {
            if (activityTemplate.getId() != null) {
                criteria.andIdEqualTo(activityTemplate.getId());
            }
            if (StringUtils.isNoneBlank(activityTemplate.getName())) {
                criteria.andNameEqualTo(activityTemplate.getName());
            }
            if (StringUtils.isNoneBlank(activityTemplate.getName())) {
                criteria.andNameEqualTo(activityTemplate.getName());
            }
        }
        criteria.andDelFlagEqualTo(ActivityTemplate.DEL_FLAG_NORMAL);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ActivityTemplate select(Integer id){
        return  activityTemplateMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据模板名称查询
     * @param name
     * @return
     */
    public ActivityTemplate selectName(String name) {
        ActivityTemplate activityTemplate = new ActivityTemplate();
        activityTemplate.setName(name);
        ActivityTemplateExample example = new ActivityTemplateExample();
        ActivityTemplateExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria,activityTemplate);
       return CollectionUtils.isNotEmpty(activityTemplateMapper.selectByExample(example)) ? activityTemplateMapper.selectByExample(example).get(0) : null;
    }
}
